<template>
  <el-dialog
    :title="title"
    v-model="model"
    width="780px"
    :before-close="closeHandle"
  >
    <el-form ref="noticeRef" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公告名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入公告标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="小区" prop="communityIds">
            <communitySelect v-model="form.communityIds" :multiple="true" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="0">草稿</el-radio>
              <el-radio :label="1">立即发布</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图标" prop="images">
            <UploadImage
              width="200px"
              :limit="1"
              :fileList="fileList"
              @fileChange="handleImageChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="公告内容" prop="noticeContent">
            <editor v-model="form.noticeContent" :min-height="192" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeHandle">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed } from "vue";
import { ElMessage } from "element-plus";
import communitySelect from "@/components/communitySelect/index.vue";
import { saveNotice, updateNoticeV2 } from "@/api/system/notice";
import UploadImage from "@/components/upload/image";
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  row: {
    type: Object,
    default: () => ({}),
  },
  communityId: {
    type: Number,
    required: true,
  },
});

const noticeRef = ref();
const isEdit = computed(() => !!props.row.id);

const emit = defineEmits(["submit"]);
const model = defineModel({
  default: false,
});

const form = ref({
  communityIds: [],
  title: "",
  status: 1,
  images: "",
  noticeContent: "",
  remark: "",
});

const rules = reactive({
  title: [{ required: true, message: "请输入公告标题" }],
  status: [{ required: true, message: "请选择状态" }],
  communityIds: [{ required: true, message: "请选择小区" }],
  images: [{ required: true, message: "请上传图标" }],
  noticeContent: [{ required: true, message: "请输入公告内容" }],
});

onMounted(() => {
  if (isEdit.value) {
    form.value = { ...props.row };
    if (props.row.images) {
      fileList.value = [
        {
          url: props.row.images,
          name: "图标",
          uid: "1",
          status: "done",
        },
      ];
    }
  }
});

const fileList = ref([]);
const handleImageChange = (fileList) => {
  fileList.value = fileList;
  form.value.images = fileList[0].url;
};

const closeHandle = () => {
  model.value = false;
};

const submitForm = () => {
  noticeRef.value.validate((valid) => {
    if (valid) {
      // type1 通知， type2 公告
      const params = { ...form.value, type: 2 };
      if (isEdit.value) {
        params.id = props.row.id;
      }
      const api = isEdit.value ? updateNoticeV2 : saveNotice;
      api(params).then(({ data }) => {
        if (data) {
          ElMessage({
            message: isEdit.value ? "编辑成功" : "添加成功",
            type: "success",
          });
          closeHandle();
          emit("submit", form.value);
        } else {
          ElMessage({
            message: "操作失败",
            type: "error",
          });
        }
      });
    }
  });
};
</script>

<style scoped>
.el-input,
.el-textarea {
  width: 100%;
}
</style>
